package com.example.administrator.bluetooth_data_transmission;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.HawkBuilder;
import com.orhanobut.hawk.LogLevel;

import java.util.Stack;
import java.util.logging.Logger;

public class MyApplication extends Application {
    private static MyApplication mApplication;

    public Stack<Activity> store;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        store = new Stack<>();
        //修改打印的tag值
        registerActivityLifecycleCallbacks(new SwitchBackgroundCallbacks());
        Hawk.init(this)
                .setEncryptionMethod(HawkBuilder.EncryptionMethod.MEDIUM)
                .setStorage(HawkBuilder.newSqliteStorage(this))
                .setLogLevel(LogLevel.FULL)
                .build();
//        Hawk.clear();

    }

    public static MyApplication getInstance() {
        return mApplication;
    }

    private class SwitchBackgroundCallbacks implements Application.ActivityLifecycleCallbacks {

        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            store.add(activity);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            store.remove(activity);
        }
    }

    /**
     * 获取当前的Activity
     *
     * @return
     */
    public Activity getCurActivity() {
        return store.lastElement();
    }
}
